package com.example.supplychainbackend.entity.quality;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "inspection_request")
@Data
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class InspectionRequestEntity {
    @Id
    private String inspectionRequestNumber;

    private String lineNumber;              //inspectionitem
    private Integer inspectionType;     //dto
    private Integer priority;               //inspectionitem
    private Integer referenceDocumentType;  //dto
    private String referenceDocumentNumber; //dto
    private String sourceDocumentNumber;    //inspectionitem
    private String sourceDocumentLineNumber;//inspectionitem
    private String materialNumber;          //inspectionitem
    private String materialName;            //inspectionitem
    private String materialDesc;     //inspectionitem
    private String unit;                    //inspectionitem
    private Integer inspectionQuantity;       //inspectionitem
    private String salesOrderNumber;        //referenceDocumentNumber->productionworkorder->planorder->salesorder
    private String salesOrderName;          //salesorder
    private String contractNumber;          //salesorder->contract
    private String contractName;            //contract
    private String batchNumber;             //inspectionitem
    private String remarks;                 //inspectionitem
    private Integer status;                 //1
    private String supplierCode;            //salesorder
    private String supplierName;            //salesorder

    private String externalDocumentNumber;  //dto

    private String containerNumber;
    private Integer inspectionMethod;
    private String samplingStandard;

    private Integer sourceDocumentType;

    private String manufacturer;
    private String manufacturerMaterialCode;
    private String requestDepartment;
    private String requestor;
    private String requestTime;
    private String inspector;
    private String inspectionDepartment;
    private String inspectionOrderNumber;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.EAGER)
    @JoinColumn(name = "inspection_request_number")
    private List<InspectionItemEntity> inspectionItems;

    @Transient
    private static AtomicInteger globalCounter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (this.inspectionRequestNumber == null || this.inspectionRequestNumber.isEmpty()) {
            // 原子递增获取序列号
            int sequence = globalCounter.getAndIncrement();

            // 格式化为IR+YYYYMMDD+五位序号
            this.inspectionRequestNumber = "IR" + DateTimeUtils.getCurrentDateForID() + String.format("%05d", sequence);
        }
    }

}
